var ue;
$(document).ready(function(){
	
	$("#contentForm").validate({
		rules: {
			CONTENT_TITLE: {
				required:true
			}
		}});
	
	queryContent();
	
	$('#editor').bind('input propertychange', function() {
		if ( $(this).val().length == 0 ){
			$("#create").addClass("disabled");
		}else{
			$("#create").removeClass("disabled");
		}
	});
	
	//实例化编辑器
	 ue = UE.getEditor('editor',{zIndex:$('.modal').css('z-index')
		 , toolbars:[
		             [ 'source', '|',
		                 'bold', 'italic', 'underline', 'strikethrough',  '|', 'forecolor', 'backcolor',  '|',
		                 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|',
		                 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
		                 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|',
		                 'link', 'unlink', '|', 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
		                 'insertimage',  '|',
		                 'horizontal', 'date', 'time']
		         ]
	 	,autoHeightEnabled:false}); 
	 
	//绑定事件
	$("#btnQuery").click(queryContent);
	
	$("#upload").click(uploadImg);
	$("#remove").click(removeImg);
	$("#save").click(save);
	$("#create").click(createHtml);
	
});



function queryContent()
{
	var formData = getFormData('queryForm');
	var column = [];
	column =  [
     {field:'CONTENT_ID',hidden:true},
     {field:'CONTENT_TITLE',title:'标题',width:"250"},
     {field:'CONTENT_IMG',title:'图片',width:"250"},
     {field:'CONTENT_DESC',title:'描述',width:"250"},
     {field:'CONTENT_URL',title:'路径',width:"250"}
     ];
	using('datagrid', function(){
		$('#userGrid').datagrid({  
			width:'auto',
			height:'auto',
//			fitColumns:true,
			nowrap: false,
			striped: true,
			singleSelect:true,
			pagination:true,
			pageNumber : 1,
			columns:[column],
		    url:appPath+'/content/listContent',  
		    queryParams : formData,
		    toolbar:[{
	       	 text:'<span class="glyphicon glyphicon-plus"></span> 增加',
	       	 handler:openModel
	        },{
	       	 text:'<span class="glyphicon glyphicon-minus"></span> 删除',
	       	 handler:delContent
	        },{
	       	 text:'<span class="glyphicon glyphicon-pencil"></span> 修改',
	       	 handler:updateContent
	        }]
		}); 
	});
}

//打开对话框
function openModel()
{
	clearFormData("contentForm");
	$("#upload").removeAttr("disabled");
	$("#fileupload").removeAttr("disabled");
	
	$("#tipalert").addClass('hide');
	$('#addContent').modal();
	$('#myModalLabel').html("添加内容");
	
}

//打开对话框，修改用户信息
function updateContent()
{
	$(".alert").addClass('hide');
	var selectedRows = $('#userGrid').datagrid('getSelections');
	if(selectedRows.length==0){
		$("#tipalert").removeClass('hide');
		$("#tipalertcontent").html('请选择数据！');
		return false;
	}
	$('#addContent').modal();
	$('#myModalLabel').html("修改内容");
	bindFormData("contentForm",selectedRows[0]);
		
}


function delContent(){

	$(".alert").addClass('hide');
	var selectedRows = $('#userGrid').datagrid('getSelections');
	if(selectedRows.length==0){
		$("#tipalert").removeClass('hide');
		$("#tipalertcontent").html('请选择数据！');
		return false;
	}
	
	//////////////////////////////////////////////////
	
	$.ajax({
		url : appPath+'/content/delContent',	
		type : 'POST',
		dataType : "json",
		data: {"data" : $.toJSONString(selectedRows[0])},
		async: false,
		error : function(xhr,status,errorThrown){
			$("#tipalert").removeClass('hide');
			$("#tipalertcontent").html('出现异常！');
		},
		success: function(data){
			$("#tipalert").removeClass('hide');
			$("#tipalertcontent").html('删除成功！');
			var rowIndex = $('#userGrid').datagrid('getRowIndex',selectedRows[0]);
			$('#userGrid').datagrid("deleteRow",rowIndex);
			queryContent();
		}
		
	});
}



function docCallback()
{
	queryContent();
}



function uploadImg(){
	  
	  using('form',function(){
			$('#contentForm').form('submit',{
		        url : appPath+"/content/upload",
		        onSubmit : function(){},
		        success : function(res){
		        	$("#CONTENT_IMG").val(res);
		        	$("#upload").attr("disabled","disabled");
		        	$("#fileupload").attr("disabled","disabled");
		        }
			});
		});
	  
}

function save(){

	$("#save").button('loading');
	using('form',function(){
		$('#contentForm').form('submit',{
	        url : appPath+"/content/save",
	        onSubmit : function(){},
	        success : function(res){
	        	$("#addContent").modal("hide");
	        	$("#tipalert").removeClass('hide');
	    		$("#tipalertcontent").html(res == "1" ? "添加内容成功！":"编辑内容成功！");
	    		$("#save").button('reset');
	    		queryContent();
	        }
		});
	});
	  
}

function removeImg(){
	
	var imgUrl = $("#CONTENT_IMG").val();
	using('form',function(){
		$('#contentForm').form('submit',{
	        url : appPath+"/content/remove?imgUrl="+imgUrl,
	        onSubmit : function(){},
	        success : function(res){
	        	if ( res != null ){
	        		$("#upload").removeAttr("disabled");
	        		$("#fileupload").removeAttr("disabled");
	        	}
	        }
		});
	});
	
}

function createHtml(){
	
	if($("#contentForm").valid() == false){
		return;
	}
	
	using('form',function(){
		$('#contentForm').form('submit',{
	        url : appPath+"/content/createHtml",
	        onSubmit : function(){},
	        success : function(res){
	        	if ( res != null ){
	        		$("#CONTENT_URL").val(res);
	        	}
	        }
		});
	});
}
	